<?php
/**
 * @date 2012-4-8
 * @author GhostWei
 */
class controller_admin_supplier_contact extends system_controller_action {
	
	private $user = null;
	private $referer = null;
	private $language = null;
	private $supplier = null;
	private $type = null;
	public function init() {
		if($this->adminLogin()) {
			return;
		}
		$this->view->language = $this->language = $this->view->language();
		$this->view->user = $this->user = system_user::getInstance()->getRow();
		$this->view->referer = $this->referer = $this->getRequest()->getHeader('referer');
		if(!!($sid = trim($this->getRequest()->getParam('s')))) {
			$concept = new supplier_information_concept();
			$this->view->supplier = $this->supplier = $concept->fetch(array('supplier.id = ?' => $sid));
		}
		$this->type = new common_addressbook_type('supplier');
				
		$this->crumbs()->addPage(system_nav_page::create()->setLabel($this->language->load('Supplier'))->setRoute('supplier/index'));		
	}
	
	public function action_addnew() {
		
		$this->render();
	}
	
	public function action_index() {
		$adapter = new system_paginator_adapter_concept(new common_addressbook_concept());
		$adapter->setCurrentPageNumber(intval($this->getRequest()->getParam('page', 1)));
		$adapter->where(array('addressbook.type = ?' => $this->type->getId()));
		if($this->supplier) {
			$adapter->where(array('addressbook.primary_id = ?' => $this->supplier->getId()));
		}
		$adapter->order("addressbook.id DESC");
		$adapter->setLimit(30);		
		$this->view->list = $list = new system_paginator($adapter);
		
		if($this->supplier) {
			$this->crumbs()->addPage(system_nav_page::create()->setLabel($this->supplier->getTitle())->setRoute('supplier/detail')->setParams(array('id' => $this->supplier->getId())));
		} else {
			$sid = $ss = array();
			foreach($list AS $row)  {
				$sid[$row->getPrimaryId()] = $row->getPrimaryId();
			}
			if(!empty($sid)) {
				$_sid = implode(', ', $sid);
				$concept = new supplier_information_concept();
				foreach($concept->fetchAll("supplier.id IN($_sid)") AS $s) {
					$ss[$s->getId()] = $s;
				}
				$this->view->ss = $ss;
			}
		}
		
		$params = $this->supplier ? array('s' => $this->supplier->getId()) : array();
		$this->crumbs()->addPage(system_nav_page::create()->setLabel($this->language->load('Contact'))->setRoute('supplier/contact/index')->setParams($params));
		$this->render();
	}
	
}